<?php

/* 会员 member */
class MemberModel extends BaseModel
{
    var $table  = 'member';
    var $prikey = 'user_id';
    var $_name  = 'member';
	
    var $_relation = array(
		'has_buyer' => array(
            'model'         => 'buyer',
            'type'          => HAS_MANY,
            'foreign_key'   => 'user_id'
        ),
		'has_sample' => array(
			'model' => 'sample',
			'type' => HAS_MANY,
			'foreign_key' => 'merchandiser_id',
		),
		'has_sourcing_list' => array(
			'model' => 'sourcing',
			'type' => HAS_MANY,
			'foreign_key' => 'sourcing_assistant_id',
		),
    	'has_consumer'=>array(
    		'model'	=> 'consumer_info',
    		'type'	=> HAS_ONE,
    		'foreign_key'=> 'user_id'
    	),
    	'has_factory'=>array(
    		'model'	=> 'factory_info',
    		'type'	=> HAS_ONE,
    		'foreign_key'=> 'user_id'
    	),
    	'has_merchant'=>array(
    		'model'	=> 'fabric_shop',
    		'type'	=> HAS_ONE,
    		'foreign_key'=> 'user_id'
    	),
    	'has_qc_name'=>array(
    		'model' => 'qcfactory',
    		'type'	=> HAS_MANY,
    		'foreign_key'=>'user_id'
    	),
    	'belongs_to_company' => array(
    		'model' => 'company',
    		'type' => BELONGS_TO,
    		'foreign_key' => 'company',
    		'reverse'       => 'has_member',
    	)
	);

    var $_autov = array(
        'user_name' => array(
            'required'  => true,
            'filter'    => 'trim',
        ),
        'password' => array(
            'required' => true,
            'filter'   => 'trim',
            'min'      => 6,
        ),
    );

    /*
     * 判断名称是否唯一
     */
    function unique($user_name, $user_id = 0)
    {
        $conditions = "user_name = '" . $user_name . "'";
        $user_id && $conditions .= " AND user_id <> '" . $user_id . "'";
        return count($this->find(array('conditions' => $conditions))) == 0;
    }

//    function drop($conditions, $fields = 'portrait')
//    {
//        if ($droped_rows = parent::drop($conditions, $fields))
//        {
//            restore_error_handler();
//            $droped_data = $this->getDroppedData();
//            foreach ($droped_data as $row)
//            {
//                $row['portrait'] && @unlink(ROOT_PATH . '/' . $row['portrait']);
//            }
//            reset_error_handler();
//        }
//        return $droped_rows;
//    }
	
	function check_powers($user_id, $role_ids)
	{
		$user = $this->get_info($user_id);
		if (!is_array($role_ids)) {
			if ($user['role'] != $role_ids) {
				return false;
			} else {
				return true;
			}
		} else {
			if (!in_array($user['role'], $role_ids)) {
				return false;
			} else {
				return true;
			}
		}
	}
	
	//查询客户的归属国家
	function get_consumer_country($conditions,$order){
		$consumer_info_mod=& m('consumer_info');
		
		$fields = " distinct(country)";
		$tables = "{$this->table} mem " .
		"LEFT JOIN {$consumer_info_mod->table} cm ON mem.user_id = cm.user_id ";
		
		/* 条件(WHERE) */
		$conditions = $this->_getConditions($conditions, true);
		/* 排序(ORDER BY) */
		if ($order)
		{
			$order = ' ORDER BY ' . $this->getRealFields($order);
		}
		
		/* 分页(LIMIT) */
		$limit && $limit = ' LIMIT ' . $limit;
		if ($limit)
		{
			$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
		}
		
		/* 完整的SQL */
		$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
		
		$country_list =$this->db->getAll($sql);
		return $country_list;
	}
	
	
	function get_fabric_list($conditions,$order,$limit){
		$fabric_mod=& m('fabric_shop');
		
		$fields="reg_time,user_name,ftype_id,phone_mob,phone_tel,is_qq,wx_sn,email,user_id";
		$tables = "{$this->table} mem ";
		
		/* 条件(WHERE) */
		$conditions = $this->_getConditions($conditions, true);
		/* 排序(ORDER BY) */
		if ($order)
		{
			$order = ' ORDER BY ' . $this->getRealFields($order);
		}
		
		/* 分页(LIMIT) */
		$limit && $limit = ' LIMIT ' . $limit;
		if ($limit)
		{
			$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
		}
		
		/* 完整的SQL */
		$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
		
		$fabric_list =$this->db->getAll($sql);
		return $fabric_list;
	}
}

?>